﻿#include "DataChosenListDialog.h"
#include "ui_DataChosenListDialog.h"
#include <QVBoxLayout>
#include <QCheckBox.h>
#include <QScrollArea.h>
#include <QLabel.h>
#include <QListView.h>
#include <QStandardItemModel>
#include <QScrollBar.h>
#include <QPushButton.h>
#include "IdaWidgetHelper.h"
#include "BaseWidget/IdaDialogMsg.h"
#include "BaseWidget/SelectAllButtonBar.h"

namespace pst
{
    DataChosenListDialog::DataChosenListDialog(QWidget* parent)
        : IdaDialogFrame(parent)
        , ui(new Ui::DataChosenListDialog())
        , m_data()
        , m_selectAllButtonBar(new BaseWidget::SelectAllButtonBar())
        //, _page1Layout(new QVBoxLayout(this))
        //, _chooseAllCheckBox(new QCheckBox(this))
        //, _listView(new QListView(this))
    {
        ui->setupUi(this);
        setWindowTitle("选择数据");

        //this->setIsFixedSize(true);
        setContentsMargins(0, 0, 0, 0);
        //this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::ApplicationModal);
        //setWindowButtonFlags(ElaAppBarType::CloseButtonHint);

        //_page1Layout->addStretch();
        //ui->listView_listView->setFixedHeight(200);
        ui->listView_listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_okButton); // 将确定设置为主按钮
        connect(m_selectAllButtonBar, &BaseWidget::SelectAllButtonBar::signal_ModifyListSelectedStatus, this, &DataChosenListDialog::slot_ModifyListSelectedStatus);
    }

    DataChosenListDialog::~DataChosenListDialog()
    {
        delete ui;
    }

    void DataChosenListDialog::setData(const QList<QString>& data)
    {
        m_data = data;
    }

    QList<QString> DataChosenListDialog::getData() const
    {
        return m_data;
    }

    void DataChosenListDialog::updateUI()
    {

        // 数据列表
        QStandardItemModel* model = new QStandardItemModel(ui->listView_listView);
        for (const auto& i : m_data)
        {
            model->appendRow(new QStandardItem(i));
        }
        ui->listView_listView->setModel(model);
        if (m_isMultipleSelected)
            ui->listView_listView->setSelectionMode(QAbstractItemView::SelectionMode::MultiSelection);
        else
            ui->listView_listView->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
        //QScrollBar* listViewFloatScrollBar = new QScrollBar(ui->listView_listView);
        //listViewFloatScrollBar->setIsAnimation(true);
        //_page1Layout->addWidget(_listView);

        // 清除选择 完成
        //QPushButton* clearButton = new QPushButton(this);
        //clearButton->setText("清除选择");
        //QPushButton* okButton = new QPushButton(this);
        //okButton->setText("完成");
        //auto clearOKLayout = new QHBoxLayout();
        //clearOKLayout->addWidget(clearButton);
        //clearOKLayout->addWidget(okButton);
        //_page1Layout->addLayout(clearOKLayout);

        /// 清除
        connect(ui->pushButton_clearButton, &QPushButton::clicked, this,
            [this]()
            {
                m_selectAllButtonBar->setCheckBoxState(0);
                ui->listView_listView->clearSelection();
            });

        /// 选择的数据发生改变
        connect(ui->listView_listView->selectionModel(), &QItemSelectionModel::selectionChanged,
            this, &DataChosenListDialog::slot_selectionChanged);

        connect(ui->pushButton_okButton, &QPushButton::clicked, this, &DataChosenListDialog::slot_okButtonClicked);
    }

    void DataChosenListDialog::setSelctedIndexs(const QList<int>& indexs)
    {
        auto model = ui->listView_listView->selectionModel();
        if (indexs.count() < 2000)
        {
            for (const auto& i : indexs)
            {
                QModelIndex index = ui->listView_listView->model()->index(i, 0);
                model->select(index, QItemSelectionModel::Select); // 此操作很耗时
            }
        }
    }

    void DataChosenListDialog::setMultipleSelected(bool isMultipleSelected)
    {
        m_isMultipleSelected = isMultipleSelected;
        if (m_isMultipleSelected)
        {
            // m_selectAllButtonBar->setGeometry(0, 0, 100, 100);
            ui->horizontalLayout->addWidget(m_selectAllButtonBar);
            this->adjustSize();
        }
        else
        {
            ui->listView_listView->setFixedHeight(204);
            this->adjustSize();
        }
    }

    void DataChosenListDialog::slot_okButtonClicked()
    {
        auto selectedIndexs = ui->listView_listView->selectionModel()->selectedIndexes();
        if (selectedIndexs.size() == 0)
        {
            EDialogMsgResult _result = IdaDialogMsg::showDialog(this, QStringLiteral("变量必须有一个取值，请重新选择数据。"), EDialogMsg::Error, QStringLiteral("错误"));
            return;
        }
        emit signal_sentChosenDataIds(selectedIndexs);

        //this->close();
        IdaDialogFrame::accept();
    }

    void DataChosenListDialog::slot_selectionChanged(
        const QItemSelection& selected, const QItemSelection& deselected)
    {
        auto selectedCount = ui->listView_listView->selectionModel()->selectedIndexes().count();
        if (selectedCount == 0)
        {
            m_selectAllButtonBar->setCheckBoxState(0,true);
        }
        else if (selectedCount == m_data.size())
        {
            m_selectAllButtonBar->setCheckBoxState(2, true);
        }
        else
        {
            m_selectAllButtonBar->setCheckBoxState(1, true);
        }
    }

    void DataChosenListDialog::slot_ModifyListSelectedStatus(int state)
    {
        if (state == 2)
        {
            ui->listView_listView->selectAll();
        }
        else if (state == 1)
        {
            
        }
        else if (state == 0)
        {
            ui->listView_listView->clearSelection();
        }
    }
}
